jenkins pipeline example

  1. this project is parameterized(string, choice)

  2. pipeline

    pipeline {
    
        agent any
    
        stages {
            stage('checkout') {
                when {
                    expression {
                        return params.action == 'deploy'
                    }
                }
                steps {
                    dir('/git/projectName/') {
                        checkout([$class: 'GitSCM',
                        branches: [[name: "${tag}"]],
                        doGenerateSubmoduleConfigurations: false,
                        extensions: [],
                        gitTool: 'Default',
                        submoduleCfg: [],
                        userRemoteConfigs: [[credentialsId: '12345678-1234-1234-1234-123456789012', url: 'https://chenshi.net/projectName']]
                        ])
                    }
                }
            }
            stage('build') {
                when {
                    expression {
                        return params.action == 'deploy'
                    }
                }
                steps {
                    dir('/git/projectName/') {
                        sh'''
                        mvn clean package -Ddocker-image.tag=${tag} -DpushImage
                        '''
                    }
                }
            }
            stage('deploy') {
                when {
                    expression {
                        return params.action == 'deploy'
                    }
                }
                steps {
                    sh '''
                    /yaml/test-projectName.sh ${tag}
                    '''
                }
            }
            stage('rollback') {
                when {
                    expression {
                        return params.action == 'rollback'
                    }
                }
                steps {
                        sh """
                        /yaml/rollback.sh ${JOB_NAME} ${tag}
                        """
                }
            }
        }
    }